<?php

declare(strict_types=1);

use owns\exception\TipsException;
use owns\enum\RespCode;
use think\Response;

// 应用公共函数
if (!function_exists('make')) {
    /**
     * 实例化类
     * @param string $name
     * @param ...$parameter
     */
    function make(string $name, ...$parameter): object
    {
        return new $name(...$parameter);
    }
}
if (!function_exists('tips')) {
    /**
     * 客户端错误提示
     * @param string $message
     * @param integer $code
     */
    function tips(string $message, $code = RespCode::ERROR): void
    {
        throw new TipsException($message, $code);
    }
}
if (!function_exists('list_fmt')) {
    /**
     * 列表数据格式
     * @param array|object $rows 列表数据
     * @param int $total 总数
     * @param string|array $other 其他
     */
    function list_fmt($rows, $total = 0, $other = ''): array
    {
        $param = request()->param();
        return [
            'rows' => $rows,
            'total' => $total,
            'page' => $param['page'] ?? 1,
            'pageSize' => $param['pageSize'] ?? 10,
            'other' => $other
        ];
    }
}
if (!function_exists('resp_header')) {
    /**
     * 响应报文
     * @param Response|null $response
     * @param array $data
     * @param string $type
     * @param integer $code
     * @param array $header
     * @param array $options
     */
    function resp_header(Response $response = null, $data = [], $type = 'json', $code = 200, $header = [], $options = []): Response
    {
        if ($response == null) {
            $response = Response::create($data, $type, $code);
        }
        if (!$header) {
            $header = [
                'Access-Control-Allow-Origin' => '*',
                'Access-Control-Allow-Methods' => 'POST,GET,PUT,DELETE,OPTIONS',
                'Access-Control-Allow-Headers' => 'X-Requested-With,Content-Type,Token,Authorization,X-Token',
                'Content-Type' => 'application/json; charset=utf-8',
                'Access-Control-Allow-Credentials' => 'false',
                'Access-Control-Max-Age' => 1800
            ];
        }
        return $response->header($header)->options($options);
    }
}
if (!function_exists('recursion')) {
    /**
     * 递归遍历
     * @param array $data 遍历的数组
     * @param int $index 顶级id
     * @param bool $child true没有子集不显示父级
     * @param string $pk tree键名
     * @param string $ck list键名
     */
    function recursion(array $data, $index = 0, $children = false, $pk = 'pid', $ck = 'id'): array
    {
        $result = [];
        foreach ($data as $item) {
            if ($index == $item[$pk]) {
                $subset = recursion($data, $item[$ck], $children, $pk, $ck);
                if ($children) {
                    if ($subset) {
                        $item['children'] = $subset;
                        $result[] = $item;
                    }
                } else {
                    $item['children'] = $subset;
                    $result[] = $item;
                }
            }
        }
        return $result;
    }
}
if (!function_exists('unique_tag')) {
    /**
     * 唯一标识获取
     */
    function unique_tag(): string
    {
        return md5(uniqid() . microtime(true));
    }
}
if (!function_exists('now_date')) {
    /**
     * 当前日期
     */
    function now_date($time = 0): string
    {
        return date('Y-m-d H:i:s', $time ?: time());
    }
}
if (!function_exists('is_mkdir')) {
    /**
     * 创建目录
     * @param string $dir
     */
    function is_mkdir(string $dir): bool
    {
        if (is_dir($dir) || mkdir($dir, 0777, true)) {
            return true;
        }
        tips('创建目录失败');
    }
}
